//
//  DetailViewController.m
//  VEW
//
//  Created by lanou3g on 15/7/24.
//  Copyright (c) 2015年 Y团. All rights reserved.
//

#import "DetailViewController.h"
#import "HomeModel.h"
#import "DataBaseManager.h"
#import "CollectTableViewController.h"

@interface DetailViewController ()
{
    UIImage *right1;
    UIImage *right2;
}

@end



@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    NSLog(@"homeData.iid = %@",_homeData.iid);
    
    webView=[[UIWebView alloc]initWithFrame: CGRectMake(0, -50, self.view.frame.size.width, self.view.frame.size.height)];
    
    webView.userInteractionEnabled = YES;
    //这个是让网页不能拉出来的属性，就是滑到下面之后不能划出留白
    webView.scrollView.bounces = NO;
    //    设置导航条的透明度;在ios7之后默认是透明的;如果导航条是透明的,显示内容的view的顶点以屏幕的原点开始;
    self.navigationController.navigationBar.translucent = NO;
    
    webView.delegate=self;
    
    [self.view addSubview:webView];
    
    NSString *urlString = [NSString stringWithFormat:@"http://m.jiemian.com/article/%@.html",_homeData.MyID];
    
    NSURL *url=[NSURL URLWithString:urlString];
    
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    
//    UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
//    
//    [self.view addSubview:view];
    UIImage *leftButtonImage=[UIImage imageNamed:@"返回"];
    
    leftButtonImage=[leftButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:leftButtonImage style:UIBarButtonItemStyleDone target:self action:@selector(doback)];
 
    
    
    right1=[UIImage imageNamed:@"收藏1"];
    
    right1=[right1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    right2=[UIImage imageNamed:@"收藏2"];
    
    right2=[right2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
//    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:right1 style:UIBarButtonItemStyleDone target:self action:@selector(collect)];
    
    BOOL isCollect = [[DataBaseManager shareInstance]isCollectNewsWithID:_homeData.MyID];

    if (YES != isCollect){
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:right1 style:UIBarButtonItemStyleDone target:self action:@selector(collect)];
    }else
    {
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:right2 style:UIBarButtonItemStyleDone target:self action:@selector(removeCollect)];
    }
    
    
}

-(void)removeCollect
{
    [[DataBaseManager shareInstance]deleteNews:_homeData];
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"取消收藏" message:nil delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
    [alertView show];

    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:right1 style:UIBarButtonItemStyleDone target:self action:@selector(collect)];
}


-(void)collect
{
//    BOOL isCollect = [[DataBaseManager shareInstance]isCollectNewsWithID:_homeData.MyID];
    
//    if (YES != isCollect) {
    [[DataBaseManager shareInstance]insertNews:_homeData];
    
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"收藏成功" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
        [alertView show];
        
        
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:right2 style:UIBarButtonItemStyleDone target:self action:@selector(removeCollect)];
//    }
    
    
    
}

//返回
-(void)doback
{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)webViewDidStartLoad:(UIWebView *)webView
{
  
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
 
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
